// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    animationData: {},
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../one/one'
    })
  },
  onPullDownRefresh() {
    this.setData({
      lifetime: []
    }); //先清空数据
    this.onLoad(); //再重新加载数据
    wx.stopPullDownRefresh(); //停止刷新操作
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canvasWidth: wx.getSystemInfoSync().screenWidth,
        canvasHeight: wx.getSystemInfoSync().screenHeight,
        canIUseGetUserProfile: true,
        lifetime: initTimeCount()
      })
    };
  },
  onShareAppMessage() {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '时间沙漏'
        })
      }, 1000)
    })
    return {
      title: '时间沙漏',
      path: '/pages/index/index',
      promise
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
})

/* 激活侧边栏人生倒计时功能 */
function initTimeCount() {
  let timelife = [{
      title: "今日已经过去",
      endTitle: "小时",
      num: 0,
      percent: "0%",
    },
    {
      title: "这周已经过去",
      endTitle: "天",
      num: 0,
      percent: "0%",
    },
    {
      title: "本月已经过去",
      endTitle: "天",
      num: 0,
      percent: "0%",
    },
    {
      title: "今年已经过去",
      endTitle: "个月",
      num: 0,
      percent: "0%",
    },
  ]; {
    let nowDate = +new Date();
    let todayStartDate = new Date(new Date().toLocaleDateString()).getTime();
    let todayPassHours = (nowDate - todayStartDate) / 1000 / 60 / 60;
    let todayPassHoursPercent = (todayPassHours / 24) * 100;
    timelife[0].num = parseInt(todayPassHours);
    timelife[0].percent = parseInt(todayPassHoursPercent);
  } {
    let weeks = {
      0: 7,
      1: 1,
      2: 2,
      3: 3,
      4: 4,
      5: 5,
      6: 6,
    };
    let weekDay = weeks[new Date().getDay()];
    let weekDayPassPercent = (weekDay / 7) * 100;
    timelife[1].num = parseInt(weekDay);
    timelife[1].percent = parseInt(weekDayPassPercent);
  } {
    let year = new Date().getFullYear();
    let date = new Date().getDate();
    let month = new Date().getMonth() + 1;
    let monthAll = new Date(year, month, 0).getDate();
    let monthPassPercent = (date / monthAll) * 100;
    timelife[2].num = date;
    timelife[2].percent = parseInt(monthPassPercent);
  } {
    let month = new Date().getMonth() + 1;
    let yearPass = (month / 12) * 100;
    timelife[3].num = month;
    timelife[3].percent = parseInt(yearPass);
  }
  return timelife;
}